package com.ruoyi.web.controller.website;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.bussiness.domain.*;
import com.ruoyi.bussiness.mapper.SysListRelatedReadMapper;
import com.ruoyi.bussiness.mapper.SysListRelatedResourceMapper;
import com.ruoyi.bussiness.service.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.resource.domain.SysBusinessResourceImage;
import com.ruoyi.resource.domain.SysBusinessResourceImageType;
import com.ruoyi.resource.domain.SysBusinessResourceModel;
import com.ruoyi.resource.domain.SysBusinessResourceVideo;
import com.ruoyi.resource.service.ISysBusinessResourceImageService;
import com.ruoyi.resource.service.ISysBusinessResourceImageTypeService;
import com.ruoyi.resource.service.ISysBusinessResourceModelService;
import com.ruoyi.resource.service.ISysBusinessResourceVideoService;
import com.ruoyi.system.domain.SysBusinessCarousel;
import com.ruoyi.system.domain.SysBussinessAboutUs;
import com.ruoyi.system.mapper.SysBusinessCarouselMapper;
import com.ruoyi.system.mapper.SysBussinessAboutUsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * @Author shining
 * @Description //TODO 桥梁网站的主要接口类，前端网站的所有接口都写到这个控制器里面，不要搞混淆了
 * @Date 17:30 2023/11/28
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/website")
public class WebsiteController extends BaseController {

    @Autowired
    private ISysBusinessDynamicTypeService sysBusinessDynamicTypeService;

    @Autowired
    private ISysBusinessDynamicService sysBusinessDynamicService;

    @Autowired
    private ISysBusinessDeptService sysBusinessDeptService;

    @Autowired
    private ISysBusinessCheckListService sysBusinessCheckListService;

    @Autowired
    private ISysBusinessResourceImageTypeService sysBusinessResourceImageTypeService;

    @Autowired
    private ISysBusinessResourceVideoService sysBusinessResourceVideoService;

    @Autowired
    private ISysBusinessResourceModelService sysBusinessResourceModelService;

    @Autowired
    private ISysBusinessResourceImageService sysBusinessResourceImageService;

    @Autowired
    private ISysBusinessPolicyService sysBusinessPolicyService;

    @Autowired
    private ISysBusinessExpertService sysBusinessExpertService;

    @Autowired
    private ISysBusinessPortfolioService sysBusinessPortfolioService;

    @Autowired
    private ISysBusinessArtworkService sysBusinessArtworkService;

    @Autowired
    private SysListRelatedReadMapper sysListRelatedReadMapper;

    @Autowired
    private SysBusinessCarouselMapper sysBusinessCarouselMapper;

    @Autowired
    private SysBussinessAboutUsMapper sysBussinessAboutUsMapper;

    @Autowired
    private SysListRelatedResourceMapper sysListRelatedResourceMapper;


    //动态类别
    @GetMapping("/dynamicTypeList")
    public TableDataInfo list(SysBusinessDynamicType sysBusinessDynamicType) {
        startPage();
        List<SysBusinessDynamicType> list = sysBusinessDynamicTypeService.selectSysBusinessDynamicTypeList(sysBusinessDynamicType);
        return getDataTable(list);
    }

    //动态列表
    @GetMapping("/dynamicList")
    public TableDataInfo list(SysBusinessDynamic sysBusinessDynamic) {
        startPage();
        List<SysBusinessDynamic> list = sysBusinessDynamicService.selectSysBusinessDynamicList(sysBusinessDynamic);
        return getDataTable(list);
    }


    //单个动态
    @GetMapping(value = "/dynamic/{dynamicId}")
    public AjaxResult getInfo(@PathVariable("dynamicId") Long dynamicId) {
        return success(sysBusinessDynamicService.selectSysBusinessDynamicByDynamicId(dynamicId));
    }


    //机构列表
    @GetMapping("/orgList")
    public TableDataInfo orgList(SysBusinessDept sysBusinessDept) {
        startPage();
        List<SysBusinessDept> list = sysBusinessDeptService.selectSysBusinessDeptList(sysBusinessDept);
        return getDataTable(list);
    }

    //获取清单列表
    @GetMapping("/checkList")
    public TableDataInfo checkList(SysBusinessCheckList checkList) {
        startPage();
        List<SysBusinessCheckList> list = sysBusinessCheckListService.selectSysBusinessCheckListList(checkList);
        return getDataTable(list);
    }


    //单个清单
    @GetMapping(value = "/checkList/{checkListId}")
    public AjaxResult getCheckListInfo(@PathVariable("checkListId") Long checkListId) {
        return success(sysBusinessCheckListService.selectSysBusinessCheckListByCheckListId(checkListId));
    }

    //单个清单得相关阅读
    @GetMapping(value = "/checkListRead/queryReadList")
    public TableDataInfo getCheckListRead(SysBusinessCheckList checkList) {
        startPage();
        SysListRelatedRead queryEntity = new SysListRelatedRead();
        queryEntity.setCheckListId(checkList.getCheckListId());
        List<SysListRelatedRead> relatedReadList = sysListRelatedReadMapper.selectSysListRelatedReadList(queryEntity);
        return getDataTable(relatedReadList);
    }

    //单个清单得相关资源
    @GetMapping(value = "/checkListRead/queryResourceList")
    public TableDataInfo getCheckListResource(SysBusinessCheckList checkList) {
        startPage();
        SysListRelatedResource queryEntity = new SysListRelatedResource();
        queryEntity.setCheckListId(checkList.getCheckListId());
        List<SysListRelatedResource> relatedReadList = sysListRelatedResourceMapper.selectSysListRelatedResourceList(queryEntity);
        return getDataTable(relatedReadList);
    }

    //单个清单得相关阅读
    @GetMapping(value = "/bridge/bigData")
    public AjaxResult getBridgeBigData() {
        return success(sysBusinessCheckListService.getBridgeBigData());
    }

    //图集列表
    @GetMapping("/imageTypeList")
    public TableDataInfo imageTypeList(SysBusinessResourceImageType sysBusinessResourceImageType) {
        startPage();
        sysBusinessResourceImageType.setStatus("1");
        List<SysBusinessResourceImageType> list = sysBusinessResourceImageTypeService.selectSysBusinessResourceImageTypeList(sysBusinessResourceImageType);
        return getDataTable(list);
    }

    //图集对应图片列表
    @GetMapping("/imageList")
    public TableDataInfo imageList(SysBusinessResourceImage sysBusinessResourceImage) {
        startPage();
        sysBusinessResourceImage.setStatus("1");
        sysBusinessResourceImage.setIsPublic("Y");
        List<SysBusinessResourceImage> list = sysBusinessResourceImageService.selectSysBusinessResourceImageList(sysBusinessResourceImage);
        return getDataTable(list);
    }

    //视频列表
    @GetMapping("/videoList")
    public TableDataInfo videoList(SysBusinessResourceVideo sysBusinessResourceVideo) {
        startPage();
        sysBusinessResourceVideo.setStatus("1");
        List<SysBusinessResourceVideo> list = sysBusinessResourceVideoService.selectSysBusinessResourceVideoList(sysBusinessResourceVideo);
        return getDataTable(list);
    }

    //单个视频
    @GetMapping(value = "/video/{videoId}")
    public AjaxResult getVideoInfo(@PathVariable("videoId") Long videoId) {
        return success(sysBusinessResourceVideoService.selectSysBusinessResourceVideoByVideoId(videoId));
    }


    //模型列表
    @GetMapping("/modelList")
    public TableDataInfo modelList(SysBusinessResourceModel sysBusinessResourceModel) {
        startPage();
        sysBusinessResourceModel.setStatus("1");
        List<SysBusinessResourceModel> list = sysBusinessResourceModelService.selectSysBusinessResourceModelList(sysBusinessResourceModel);
        return getDataTable(list);
    }

    //单个模型
    @GetMapping(value = "/model/{modelId}")
    public AjaxResult getModelInfo(@PathVariable("modelId") Long modelId) {
        return success(sysBusinessResourceModelService.selectSysBusinessResourceModelByModelId(modelId));
    }

    //政策列表
    @GetMapping("/policyList")
    public TableDataInfo list(SysBusinessPolicy sysBusinessPolicy) {
        startPage();
        List<SysBusinessPolicy> list = sysBusinessPolicyService.selectSysBusinessPolicyList(sysBusinessPolicy);
        return getDataTable(list);
    }

    //学者风采
    @GetMapping("/expertList")
    public AjaxResult expertList() {
        SysBusinessExpert queryEntity = new SysBusinessExpert();
        queryEntity.setStatus("1");
        List<SysBusinessExpert> list = sysBusinessExpertService.selectSysBusinessExpertList(queryEntity);
        return success(list);
    }

    //作品集接口
    @GetMapping("/portfolioList")
    public AjaxResult portfolioList() {
        SysBusinessPortfolio queryEntity = new SysBusinessPortfolio();
        queryEntity.setStatus("1");
        List<SysBusinessPortfolio> list = sysBusinessPortfolioService.selectSysBusinessPortfolioList(queryEntity);
        //查询作品集
        SysBusinessArtwork sysBusinessArtwork = new SysBusinessArtwork();
        sysBusinessArtwork.setStatus("1");
        sysBusinessArtwork.setPortfolioIdList(list.stream().map(p -> p.getId()).collect(Collectors.toList()));
        List<SysBusinessArtwork> artworkList = sysBusinessArtworkService.selectSysBusinessArtworkList(sysBusinessArtwork);
        JSONObject jsObj = new JSONObject();
        jsObj.put("portfolioList", list);
        jsObj.put("artworkTotalNum", artworkList == null ? 0 : artworkList.size());
        return success(jsObj);
    }

    //获取作品列表
    @GetMapping("/artworkList")
    public AjaxResult artworkList(SysBusinessArtwork sysBusinessArtwork) {
        SysBusinessPortfolio sysBusinessPortfolio = sysBusinessPortfolioService.selectSysBusinessPortfolioById(sysBusinessArtwork.getPortfolioId());
        sysBusinessArtwork.setStatus("1");
        List<SysBusinessArtwork> list = sysBusinessArtworkService.selectSysBusinessArtworkList(sysBusinessArtwork);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("portfolioName", sysBusinessPortfolio == null ? "" : sysBusinessPortfolio.getPortfolioName());
        jsonObject.put("artworkList", list);
        return success(jsonObject);
    }

    //获取作品列表
    @GetMapping("/getArtworkById")
    public AjaxResult getArtworkById(SysBusinessArtwork sysBusinessArtwork) {
        SysBusinessArtwork responseArtwork = sysBusinessArtworkService.selectSysBusinessArtworkById(sysBusinessArtwork.getId());
        if (responseArtwork != null) {
            SysBusinessPortfolio sysBusinessPortfolio = sysBusinessPortfolioService.selectSysBusinessPortfolioById(sysBusinessArtwork.getPortfolioId());
            responseArtwork.setPortfolioName(sysBusinessPortfolio == null ? "" : sysBusinessPortfolio.getPortfolioName());
        }
        return success(responseArtwork);
    }

    @GetMapping("/getLastOrNextArtwork")
    public AjaxResult getLastOrNextArtwork(SysBusinessArtwork sysBusinessArtwork) {
        SysBusinessArtwork responseArtwork = sysBusinessArtworkService.getLastOrNextArtwork(sysBusinessArtwork);
        return success(responseArtwork);
    }


    @GetMapping("/getCarouselList")
    public AjaxResult getCarouselList() {
        SysBusinessCarousel queryEntity = new SysBusinessCarousel();
        queryEntity.setStatus("0");
        List<SysBusinessCarousel> carouselList = sysBusinessCarouselMapper.selectSysBusinessCarouselList(queryEntity);
        return success(carouselList);
    }

    @GetMapping("/getAboutUs")
    public AjaxResult getAboutUs() {
        SysBussinessAboutUs queryEntity = new SysBussinessAboutUs();
        List<SysBussinessAboutUs> carouselList = sysBussinessAboutUsMapper.selectSysBussinessAboutUsList(queryEntity);
        return success(StringUtils.isEmpty(carouselList) ? null : carouselList.get(0));
    }

    //搜索
    @GetMapping("/getSearch")
    public AjaxResult getSearch(@RequestParam("searchStr") String searchStr) {
        List<Map<String, Object>> carouselList = sysBusinessDynamicService.getSearch(searchStr);
        return success(carouselList);
    }


}
